﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;


namespace CommChat
{

    public partial class FrmMain : Form
    {
        SerialPort _sp = null;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            foreach (string portName in SerialPort.GetPortNames())
                cmbPuertos.Items.Add (portName);

            if (cmbPuertos.Items.Count > 0)
                cmbPuertos.SelectedIndex = 0;

            cmbBdrates.SelectedIndex = 0;
        }

        private void btnAbrir_Click(object sender, EventArgs e)
        {
            if (cmbPuertos.Items.Count == 0)
                return;

            string  puerto      = (string) cmbPuertos.Items[cmbPuertos.SelectedIndex];
            int     baudrate    = int.Parse((string)cmbBdrates.Items[cmbBdrates.SelectedIndex]);

            _sp = new SerialPort(puerto, baudrate);

            _sp.Open();
            _sp.DataReceived += new SerialDataReceivedEventHandler(_sp_DataReceived);
            btnAbrir.Enabled = false;
        }

        void _sp_DataReceived (object sender, SerialDataReceivedEventArgs e)
        {
            int aLeer = _sp.BytesToRead;

            for (int i = 0; i < aLeer; ++i)
            {
                int byteLeido = _sp.ReadByte ();

                if (chkHex.Checked)
                {
                    mostrar (byteLeido.ToString("X2") + " ");
                }
                else
                {
                    mostrar ((char)(byte) byteLeido);
                }
            }

            if (chkHex.Checked)
            {
                mostrar("\r\n");
            }
        }

        private void mostrar (string s)
        {
            Invoke(new MethodInvoker(() =>
            {
                txtRecibe.Text += s;
                txtRecibe.SelectionStart = txtRecibe.Text.Length;
                txtRecibe.ScrollToCaret();
            }));
        }

        private void  mostrar (char c)
        {
            Invoke(new MethodInvoker(() =>
                {
                    txtRecibe.Text += c;
                    txtRecibe.SelectionStart = txtRecibe.Text.Length;
                    txtRecibe.ScrollToCaret();
                }));
        }

        private void btnMandar_Click(object sender, EventArgs e)
        {
            string s = txtMandar.Text ;

            byte[] buff = Encoding.ASCII.GetBytes(s);

            _sp.Write(buff, 0, buff.Length);
        }

        private void btnMandarConCR_Click(object sender, EventArgs e)
        {
            string s = txtMandar.Text;

            byte[] buff = Encoding.ASCII.GetBytes(s);

            _sp.Write(buff, 0, buff.Length);
            _sp.Write(new byte[] { 10 }, 0, 1);
        }

        private void btnMandarCR_Click(object sender, EventArgs e)
        {
            byte[] buff = { 10 };
            _sp.Write(buff, 0, buff.Length);
        }

        private void btnMandarLF_Click(object sender, EventArgs e)
        {
            byte[] buff = { 13 };
            _sp.Write(buff, 0, buff.Length);
        }

        private void btnMandarCtrlZ_Click(object sender, EventArgs e)
        {
            byte[] buff = { 26 };
            _sp.Write(buff, 0, buff.Length);
        }

        private void btnHexMandar_Click(object sender, EventArgs e)
        {
            string s        = txtMandar.Text;
            string [] partes= s.Split(' ');

            List<byte> bytes = new List<byte>();

            foreach (string parte in partes)
                bytes.Add ( byte.Parse(parte, System.Globalization.NumberStyles.HexNumber));

            byte [] buff = bytes.ToArray();

            _sp.Write(buff, 0, buff.Length);
            txtMandar.Text = "";
        }
    }
}
